import { CreepsConfig } from '../../game/global'
import { LogHelper } from "../../utils/logHelper";

export class outerOreCarryTask implements IOuterOreTask{
  run(room: Room, closestRoom: Room): void {

    // 控制每10tick检查一次
    if(!room){
      return;
    }
    if(!room.memory.outerOreCarryCheckLastTime || ((Game.time - room.memory.outerOreCarryCheckLastTime) > 10)){
      // 取房间内可采集的资源
      const sources = room.find(FIND_SOURCES);
      for(const source of sources) {
        
        // 查找当前房间有没有外矿采集的虫子
        const roomCreeps = room.find(FIND_MY_CREEPS).filter((creep) => {
          return creep.memory.source && creep.memory.source.id === source.id && creep.memory.role === 'outerOreCarry'
        });
        // 查找附近房间有没有外矿采集的虫子
        const closestRoomCreeps = closestRoom.find(FIND_MY_CREEPS).filter((creep) => {
          return creep.memory.source && creep.memory.source.id === source.id && creep.memory.role === 'outerOreCarry'
        });
        // 查找孵化队列中有没有外矿采集的虫子
        if(!closestRoom.memory.spawnQueue){
          closestRoom.memory.spawnQueue = new Array<{ id:number,role:string,body:BodyPartConstant[],memory:CreepMemory }>();
        }
        const queueCreeps = closestRoom.memory.spawnQueue.filter((creep)=>{
          return creep.memory.source && creep.memory.source.id === source.id && creep.memory.role === 'outerOreCarry'
        });
        
        // 当前搬运工的总数
        const outerOreCarryCount = roomCreeps.length+closestRoomCreeps.length+queueCreeps.length;
        const outerOreCarryQuantity = 1;

        if(outerOreCarryCount < outerOreCarryQuantity && !closestRoom.find(FIND_MY_SPAWNS)[0].spawning){
            const controllerLevel = closestRoom.controller?.level;
            if(controllerLevel){
              let role = "outerOreCarry";
              let body = closestRoom.calculateCreepBody({
                [MOVE]:0.5,
                [CARRY]:0.5,
              },800);
              for (let index = 0; index < (outerOreCarryQuantity-outerOreCarryCount); index++) {
                if(body){
                  closestRoom.spawnQueueAdd(role,body,{ role:role, bornRoom:closestRoom.name, source: { id:source.id,pos:source.pos }});
                  LogHelper.log(`外矿房间管理[${room.name}] 申请孵化 ${role}`);
                }
              }
            }
      }
      room.memory.outerOreCarryCheckLastTime = Game.time;
    }
  }
  }

  private getBody(level:number):BodyPartConstant[] | undefined {
    let body:BodyPartConstant[] = [];
    switch (level) {
      case 1:
        body = [MOVE,MOVE,MOVE,CARRY,CARRY,CARRY] //300
        break;
      case 2:
        body = [MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY] //550
        break;
      case 3:
        body = [MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY] //800
        break;
      case 4:
        body = [MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE
               ,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY] //1300
        break;
      default:
        body = [MOVE,MOVE,MOVE,CARRY,CARRY,CARRY]
        break;
    }
    return body;
  }
}